/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;


import edu.upc.clase.demo.dao.PlanBusquedaDao;
import edu.upc.clase.demo.entity.PlanBusqueda;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
 *
 * @author RICARDO
 */
@Repository
public class PlanBusquedaDaoImpl extends SimpleJdbcDaoSupport implements PlanBusquedaDao {
      private static Logger log = LoggerFactory.getLogger(PlanBusquedaDaoImpl.class);
    
    @Autowired
    public PlanBusquedaDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
    
   @Override
    public PlanBusqueda buscar(String titulo,int tiempo, double mo_total, double mo_margen, double mo_roi) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select `titulo` from plan_negocio where titulo=? and tiempo>? and tiempo<? and mo_total<? and "
                + "mo_total<? and mo_margen>? and mo_margen<? and mo_roi>? and mo_roi<?",
                    new BeanPropertyRowMapper<PlanBusqueda>(PlanBusqueda.class),titulo,tiempo,mo_total,mo_margen,mo_roi );
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<PlanBusqueda> buscar() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<PlanBusqueda> listarPlanesDisponibles(int id_plan) {
        try {
            Map<String,String> parametros = new HashMap<String,String>();
            parametros.put("id_plan","%"+id_plan+"%");
            return getSimpleJdbcTemplate().query(
                    "select * from plan_negocio where id_plan=:id_plan AND estado=:1",
                    new BeanPropertyRowMapper<PlanBusqueda>(PlanBusqueda.class),parametros);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }        
    }

    @Override
    public PlanBusqueda buscar(int id_plan) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select * from plan_negocio where id_plan=:id_plan AND estado=:1",
                    new BeanPropertyRowMapper<PlanBusqueda>(PlanBusqueda.class),id_plan);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    


}

